#include "settingctl.h"
#include "graphicsPanel.h"
#include <QString>
#include <QVariant>
#include <QSettings>


SettingCtl::SettingCtl(QWidget *parent, GraphicsPanel* gp, int no) 
{
	graphicspanel = gp;
	panel_no = no;
  

}

SettingCtl::~SettingCtl()
{
     writeSettings();

}

void SettingCtl::setCtlpanel(

	    	QPushButton  *_exec,
	    	QComboBox    *_align1,
	    	QComboBox    *_align2,
	    	QLineEdit    *_scene_x,
	    	QLineEdit    *_scene_y,
	    	QLineEdit    *_scene_w,
	    	QLineEdit    *_scene_h,
	    	QPushButton  *_bgc,
	    	QPushButton  *_fgc,
	    	QComboBox    *_draw_ptn,
	    	QLineEdit    *_draw_size,
	    	QLineEdit    *_scale_x,
	    	QLineEdit    *_scale_y,
	    	QLineEdit    *_rotay  )

{
	
   exec           = _exec;
   align1         = _align1;
   align2         = _align2;
   scene_x        = _scene_x;
   scene_y        = _scene_y;
   scene_w        = _scene_w;
   scene_h        = _scene_h;
   bgc            = _bgc;
   fgc            = _fgc;
   draw_ptn       = _draw_ptn;
   draw_size      = _draw_size;
   scale_x        = _scale_x;
   scale_y        = _scale_y;
   rotay          = _rotay;

   align1->addItem(QString("Top"));
   align1->addItem(QString("Bottom"));
   align2->addItem(QString("Left"));
   align2->addItem(QString("Right"));
   draw_ptn->addItem(QString("pt1"));
   draw_ptn->addItem(QString("pt2"));

   readSettings();

   QObject::connect(bgc, SIGNAL(clicked()), this, SLOT(slotBgcSet()));
   QObject::connect(exec, SIGNAL(clicked()), this, SLOT(slotDrawExec()));
 
}

void SettingCtl::slotDrawExec()
{
 // Align set
	if (align1->currentText() == "Top") 
	{
      if (align2->currentText() == "Left")
	  {
       graphicspanel->setAlign (Qt::AlignLeft | Qt::AlignTop);
	  } else {
       graphicspanel->setAlign (Qt::AlignRight | Qt::AlignTop);
	  }
	} else  // "Bottom" 
	{
      if (align2->currentText() == "Left")
	  {
       graphicspanel->setAlign (Qt::AlignLeft | Qt::AlignBottom);
	  } else {
       graphicspanel->setAlign (Qt::AlignRight | Qt::AlignBottom);
	  }
	}

	// Scene Rect set
	graphicspanel->setScene(
         QVariant(scene_x->text()).toInt(),
         QVariant(scene_y->text()).toInt(),
         QVariant(scene_w->text()).toInt(),
         QVariant(scene_h->text()).toInt()
		 );

	// Background Color set
    QPalette p = bgc->palette();
 
	QBrush bg( p.color(QPalette::Button));
    graphicspanel->setBackgroundBrash(bg); 
    
	// Drawing
	graphicspanel->draw(QVariant(draw_size->text()).toInt());

	// Scalling & Rotay

	int sx = QVariant(scale_x->text()).toInt();
	int sy = QVariant(scale_y->text()).toInt();
    int rd = QVariant(rotay->text()).toInt();
    
	if ( sx != 1 || sy != 1) graphicspanel->setScale( sx, sy);
    if ( rd != 0 ) graphicspanel->setRotate( rd );
    
}


void SettingCtl::slotBgcSet()
{
  QColor color = QColorDialog::getColor();
  QPalette pal = bgc->palette();
  pal.setColor(QPalette::Button,color);
  bgc->setPalette(pal);
}

void SettingCtl::writeSettings()
 {
/*
   align1         = _align1;
   align2         = _align2;
   scene_x        = _scene_x;
   scene_y        = _scene_y;
   scene_w        = _scene_w;
   scene_h        = _scene_h;
   bgc            = _bgc;
   fgc            = _fgc;
   draw_ptn       = _draw_ptn;
   draw_size      = _draw_size;
   scale_x        = _scale_x;
   scale_y        = _scale_y;
   rotay          = _rotay; 
*/

  // C:\Users\gusagusa\AppData\Roaming\Qtdevsoft\Psmem.ini

  QSettings settings( QSettings::IniFormat,QSettings::UserScope,
	                 "Qtdevsoft","gtest",this);

  settings.beginGroup(QString("Ctl_%1").arg(panel_no));
  settings.setValue("align1"   , align1->currentText());
  settings.setValue("align2"   , align2->currentText()); 
  settings.setValue("scene_x"  , scene_x->text());
  settings.setValue("scene_y"  , scene_y->text());
  settings.setValue("scene_w"  , scene_w->text());
  settings.setValue("scene_h"  , scene_h->text());

    QPalette pl1 = bgc->palette();
 	QColor cl1 = pl1.color(QPalette::Button);
    QString cstr1 = cl1.name(); 
  settings.setValue("bgc"  , cstr1);

    QPalette pl2 = fgc->palette();
 	QColor cl2 = pl2.color(QPalette::Button);
    QString cstr2 = cl2.name(); 
  settings.setValue("fgc",  cstr2);

  settings.setValue("draw_ptn"  , draw_ptn->currentText());
  settings.setValue("draw_size", draw_size->text());
  settings.setValue("scale_x", scale_x->text());
  settings.setValue("scale_y", scale_y->text());
  settings.setValue("rotay", rotay->text());


  settings.endGroup();  

   
}


void SettingCtl::readSettings()
{


  QSettings settings( QSettings::IniFormat,QSettings::UserScope,
	                 "Qtdevsoft","gtest",this);

  settings.beginGroup(QString("Ctl_%1").arg(panel_no));

  int index;

  index = align1->findText( settings.value("align1"   , "Top").toString());
  align1->setCurrentIndex ( index ); 
  index = align2->findText( settings.value("align2"   , "Left").toString());
  align2->setCurrentIndex ( index ); 

  scene_x->insert( settings.value("scene_x"   , "0").toString());
  scene_y->insert( settings.value("scene_y"   , "0").toString());
  scene_w->insert( settings.value("scene_w"   , "200").toString());
  scene_h->insert( settings.value("scene_h"   , "100").toString());

  QString cstr1 = settings.value("bgc"   , "#000000").toString();
    QPalette palette1;
    QColor color1;
    color1.setNamedColor(cstr1);
    palette1.setColor( QPalette::Button, color1);
    bgc->setPalette(palette1);


  QString cstr2 = settings.value("fgc"   , "#000000").toString();
    QPalette palette2;
    QColor color2;
	color2.setNamedColor(cstr2);
    palette2.setColor( QPalette::Button, color2);
    fgc->setPalette(palette2);

  index = draw_ptn->findText( settings.value("draw_ptn"   , "pt1").toString());
  draw_ptn->setCurrentIndex ( index ); 

  draw_size->insert( settings.value("draw_size"   , "100").toString());
  scale_x->insert( settings.value("scale_x"   , "1").toString());
  scale_y->insert( settings.value("scale_y"   , "1").toString());
  rotay->insert( settings.value("rotay"   , "0").toString());
 
  settings.endGroup();  
 
}



	    

